<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('模块名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('模块名称')" maxlength="20" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('模块类型')" prop="type">
            <el-select v-model="form.type" filterable :placeholder="$t('请选择')">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('组件地址')" prop="component">
            <el-input v-model="form.component" :placeholder="$t('组件地址')" maxlength="200" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('分类')" prop="group">
            <el-select v-model="form.group" filterable :placeholder="$t('请选择')">
              <el-option
                v-for="dict in groupOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="system:module" />
    </div>
  </div>
</template>

<script>
    import { saveHomeModule, getHomeModule } from '@/api/system/home/homeModule'
    export default {
        data() {
            return {
              // 模块类型字典
              typeOptions: [], // 表单参数
              form: {},
              loading: false,
              // 表单校验
              rules: {
                name: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                type: [
                  { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                ],
                component: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                group: [
                  { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                ]
              },
              // 分类字典
              groupOptions: []
            }
        },
        created() {
            this.getDicts('home_module_type').then(response => {
                this.typeOptions = response.data
            })
            this.getDicts('home_group').then(response => {
              this.groupOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getHomeModule(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                name: undefined,
                type: undefined,
                component: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        saveHomeModule(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                                this.loading = false
                            }
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
